home *** CD-ROM | disk | FTP | other *** search
- #include <support.h>
- #include <string.h>
- #include <osbind.h>
- #include <mintbind.h>
- #include <stdarg.h>
- #include <unistd.h>
- #include "global.h"
-
- #ifdef DEBUG
-
- #ifdef DAEMON
- # define PROLOG "GSDaemon: "
- #else
- # ifdef DRIVER
- # define PROLOG "GSDriver: "
- # else
- # define PROLOG ""
- # endif
- #endif
-
- static char DEBUG_LOG[] = "A:\\STIK_CFG\\GLUESTIK.LOG";
- static char DEBUG_OLD[] = "A:\\STIK_CFG\\GLUESTIK.OLD";
-
- typedef char *charp;
- typedef void *voidp;
-
- void debug(const char *fmt, ...)
- {
- va_list ap;
- register const char *s;
- char *t;
- void *v;
- char buf[20];
- long l;
- int fd;
- static int first_time = 1;
-
- Psemaphore(2, DEBUG_SEMAPHORE, -1L);
- va_start(ap, fmt);
- if (first_time) {
- DEBUG_LOG[0] = DEBUG_OLD[0] = 'A' + Dgetdrv();
- #ifndef DRIVER
- Fdelete(DEBUG_OLD);
- l = Frename(0, DEBUG_LOG, DEBUG_OLD);
- if (l == 0) {
- Cconws("Preserved old debug log ");
- Cconws(DEBUG_OLD);
- Cconws("\r\n");
- }
- l = Fcreate(DEBUG_LOG, 0);
- if (l < 0) {
- Cconws("Unable to create debug log ");
- Cconws(DEBUG_LOG);
- Cconws("\r\n");
- return;
- }
- Cconws("Debug log ");
- Cconws(DEBUG_LOG);
- Cconws(" created\r\n");
- fd = (int)(short)l;
- #endif
- first_time = 0;
- }
- #ifndef DRIVER
- else
- #endif
- {
- l = Fopen(DEBUG_LOG, 2);
- if (l < 0) {
- Cconws("Unable to open debug log ");
- Cconws(DEBUG_LOG);
- Cconws("\r\n");
- return;
- }
- fd = (int)(short)l;
- Fseek(0L, fd, SEEK_END);
- }
-
- Fwrite(fd, strlen(PROLOG) + 4, PROLOG "pid ");
- _itoa(getpid(), buf, 10);
- Fwrite(fd, strlen(buf), buf);
- Fwrite(fd, 3, ": ");
-
- for (s = fmt; *s; s++) {
- switch (*s) {
- case 's':
- t = va_arg(ap, charp);
- Fwrite(fd, strlen(t), t);
- break;
- case 'S':
- l = (long)va_arg(ap, int);
- t = va_arg(ap, charp);
- Fwrite(fd, l, t);
- break;
- case 'i':
- case 'd':
- l = (long)va_arg(ap, int);
- _ltoa(l, buf, 10);
- Fwrite(fd, strlen(buf), buf);
- break;
- case 'x':
- Fwrite(fd, 2, "0x");
- l = (long)va_arg(ap, unsigned int);
- _ltoa(l, buf, 16);
- Fwrite(fd, strlen(buf), buf);
- break;
- case 'l':
- l = va_arg(ap, long);
- _ltoa(l, buf, 10);
- Fwrite(fd, strlen(buf), buf);
- break;
- case 'X':
- Fwrite(fd, 2, "0x");
- l = va_arg(ap, unsigned long);
- _ultoa(l, buf, 16);
- Fwrite(fd, strlen(buf), buf);
- break;
- case 'p':
- Fwrite(fd, 2, "0x");
- v = va_arg(ap, voidp);
- _ultoa((unsigned long)v, buf, 16);
- Fwrite(fd, strlen(buf), buf);
- break;
- case 'c':
- buf[0] = va_arg(ap, int);
- Fwrite(fd, 1, buf);
- break;
- default:
- Fwrite(fd, 3, "<<<");
- Fwrite(fd, 1, s);
- Fwrite(fd, 4, "?>>>");
- break;
- }
- }
- Fwrite(fd, 2, "\r\n");
- Fclose(fd);
- Psemaphore(3, DEBUG_SEMAPHORE, 0L);
- }
-
- #endif /* DEBUG */
-